package com.fangsheng.rocketmq.general;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * @创建人: 放生
 * @创建时间: 2021/7/31
 * @描述: 同步消息
 */
public class SyncProduct {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        //创建一个producer,  参数为你Producer Group名称，其实就是ID 同一个ID表示为同一个组
        DefaultMQProducer producer = new DefaultMQProducer("pg");
        //指定nameServer地址
        producer.setNamesrvAddr("192.168.10.11:9876");
//        其他设置项
//        producer.setRetryTimesWhenSendAsyncFailed(3);
//        producer.setRetryTimesWhenSendFailed(3);
//        producer.setSendMsgTimeout(5000);
        producer.start();
        for (int i = 0; i < 10; i++) {
            byte[] body = ("Hi555," + i).getBytes();
            //someTopic 如果该主题没有提前创建，也可以不创建，默认会自动创建，并且默认也会为该主题创建四个队列
            Message msg = new Message("someTopic","someTag",body);
//            msg.setKeys("key"+i); 可设置key 也可以不设置
            //发送消息
            SendResult sendResult = producer.send(msg);
            System.out.println(sendResult);
        }
        //关闭 producer
        producer.shutdown();



    }


}
